<?php 

namespace app\applet;

use think\App;

class BaseController extends \app\BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 页数
     * @var int
     */
    protected $page = 1;

    /**
     * 页码
     * @var int
     */
    protected $pagesize = 10;

    /**
     * @var array
     */
    protected $appletId = 1;

    /**
     * @var array
     */
    protected $get = [];

    /**
     * @var array
     */
    protected $post = [];

    /**
     * @var null
     */
    protected $admin = null;

    /**
     * @var null
     */
    protected $token = null;

    protected $user = null;

	public function __construct(App $app)
	{
		parent::__construct($app);
		$this->initialize();
	}

	public function initialize()
	{
		$this->page = $this->request->get('page')?:1;

        $this->pagesize = $this->request->get('pagesize')?:10;

        $this->post = $this->request->post();

        $this->get = $this->request->get();

        $this->appletId = $this->request->header('AppletId')?:1;

        $authHeader = $this->request->header('Authorization');
        if (!empty($authHeader) && preg_match("/^Bearer\\s+(.*?)$/", $authHeader, $matches)) {
            $token = $matches[1];
        } else {
            $token = $this->request->get("token", "string", null);
        }
        $user = $this->app->userService->checkToken($token);
        if ($token && $user ) {
           $this->token = $token;
           $this->user = $user;
        }
	}
}